﻿using ConfExpert.Forms.Base;
using DevExpress.Data;
using DevExpress.XtraPivotGrid;
using DevTeam;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class DonusAnaliz : XForm
    {
        public DonusAnaliz()
        {
            InitializeComponent();
        }

        BindingSource bs;
        private void DonusAnaliz_Load(object sender, EventArgs e)
        {
            bs = gridView1.Init("GetDonusAnalizRFID");
            gridView1.FormatAsDate("Tarih");

            gridView1.FormatAsNumber(0, "RFIDsizAdet", "RFIDliAdet", "CikilanAdet", "RFIDsizMt", "RFIDliMt", "CikilanMt");
            gridView1.AddSummary("RFIDsizAdet", "RFIDliAdet", "CikilanAdet", "RFIDsizMt", "RFIDliMt", "CikilanMt");
            InitPivot(pivotGridControl1, bs);

            splitContainerControl1.AutoSize();
            ExpandCollapse(splitContainerControl1, AcKapa.Collapse);
            pivotGridControl1.BestFit();
        }

        void pivotGridControl1_CustomAppearance(object sender, PivotCustomAppearanceEventArgs e)
        {
            if (e.Focused)
                e.Appearance.BackColor = e.Appearance.BackColor2 = Color.YellowGreen;   // Color.FromArgb(255, 192, 0);
        }

        void pivotGridControl1_CellClick(object sender, DevExpress.XtraPivotGrid.PivotCellEventArgs e)
        {
            gridControl1.DataSource = e.CreateDrillDownDataSource();
            gridView1.BestFitColumns();
            ExpandCollapse(splitContainerControl1, (gridView1.RowCount > 0 ? AcKapa.Expand : AcKapa.Collapse));
            gridView1.MoveFirst();
        }

        bool PivotInitialized = false;
        void InitPivot(PivotGridControl pgc, BindingSource bs)
        {
            pgc.DataSource = bs;
            if (!PivotInitialized)
            {
                pgc.OptionsCustomization.CustomizationFormStyle = DevExpress.XtraPivotGrid.Customization.CustomizationFormStyle.Excel2007;
                pgc.FieldValueDisplayText += new PivotFieldDisplayTextEventHandler(pivotGrid_FieldValueDisplayText);
                pgc.CustomAppearance += new PivotCustomAppearanceEventHandler(pivotGridControl1_CustomAppearance);
                pgc.CellClick += new PivotCellEventHandler(pivotGridControl1_CellClick);
                //pgc.CustomUnboundFieldData += pgc_CustomUnboundFieldData;


                pgc.BeginUpdate();

                PivotGridField[] fields2 = new PivotGridField[2];
                PivotGridField fieldYear = new PivotGridField("Tarih", PivotArea.ColumnArea);
                PivotGridField fieldQuarter = new PivotGridField("Tarih", PivotArea.ColumnArea);

                pgc.Fields.Add(fieldYear);
                fieldYear.GroupInterval = PivotGroupInterval.DateYear;
                fieldYear.Caption = "Yıl";

                pgc.Fields.Add(fieldQuarter);
                fieldQuarter.GroupInterval = PivotGroupInterval.DateQuarterYear;
                fieldQuarter.Caption = "Çeyrek";

                fields2[0] = fieldYear;
                fields2[1] = fieldQuarter;

                fields2[0].ExpandedInFieldsGroup = fields2[1].ExpandedInFieldsGroup = true;
                pgc.Groups.Add(fields2, "Tarih");

                AddNewField(pgc, "AtelyeId", PivotArea.RowArea).FieldEdit = RI(Rp.Atelye);

                AddNewField(pgc, "RFIDsizAdet", PivotArea.DataArea).Caption = "RFIDsiz";
                AddNewField(pgc, "RFIDliAdet", PivotArea.DataArea).Caption = "RFIDli";

                PivotGridField fieldToplam = new PivotGridField("Toplam", PivotArea.DataArea);
                fieldToplam.Caption = fieldToplam.FieldName = "Toplam";
                fieldToplam.UnboundType = UnboundColumnType.Integer;
                fieldToplam.UnboundExpression = "ISNULL(RFIDsizAdet,0) + ISNULL(RFIDliAdet,0)";
                pgc.Fields.Add(fieldToplam);

                PivotGridField fieldOran = new PivotGridField("Oran", PivotArea.DataArea);
                fieldOran.UnboundType = UnboundColumnType.Decimal;
                fieldOran.Caption = "RFIDli Dönüş Oranı";
                fieldOran.UnboundExpression = "RFIDliAdet / Toplam";
                pgc.Fields.Add(fieldOran);

                fieldOran.CellFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
                fieldOran.CellFormat.FormatString = "P1";
                fieldOran.UnboundExpressionMode = UnboundExpressionMode.UseSummaryValues;

                pgc.Fields["RFIDliAdet"].Visible = pgc.Fields["RFIDsizAdet"].Visible = pgc.Fields["Toplam"].Visible = false;

                foreach (PivotGridField pgf in pgc.Fields)
                {
                    pgf.SortOrder = PivotSortOrder.Ascending;
                    pgf.Options.AllowSort = DevExpress.Utils.DefaultBoolean.True;
                }


                /*
                 * Error satırlarından dolayı hata verdi, kapattım
                 * 
                PivotGridFormatRule newRule = new PivotGridFormatRule();
                pgc.FormatRules.Add(newRule);

                // Sets a Measure.
                newRule.Measure = pgc.Fields["Oran"];

                // Creates a new Settings object.
                FormatRuleFieldIntersectionSettings newClass = new FormatRuleFieldIntersectionSettings();
                newRule.Settings = newClass;

                // Sets the Settings parameters.
                newClass.Column = pgc.Fields["Tarih"];
                newClass.Row = pgc.Fields["AtelyeId"];

                // Creates a new Rule object and defines its type.
                FormatConditionRuleDataBar formatConditionRuleDataBar = new FormatConditionRuleDataBar();
                newRule.Rule = formatConditionRuleDataBar;

                // Sets the Rule parameters.
                formatConditionRuleDataBar.PredefinedName = "Yellow Gradient";
                */

                pgc.EndUpdate();
                //pgc.BestFit();

                PivotInitialized = true;
            }
        }

        void pgc_CustomUnboundFieldData(object sender, CustomFieldDataEventArgs e)
        {
            if (e.Field.FieldName == "Oran")
            {
                decimal RFIDliAdet = (e.GetListSourceColumnValue("RFIDliAdet")).ToDecimal();
                int Toplam = (e.GetListSourceColumnValue("Toplam")).ToInt();
                if (Toplam > 0)
                    e.Value = RFIDliAdet / Toplam;

                /*
                                e.Value = new ExpressionEvaluator(controller.GetDescriptorCollection(), "RFIDliAdet / Toplam")
                                    .Evaluate(controller.ListSource[e.ListSourceRowIndex]);
                 */
            }
        }
    }
}
